package cn.wolfcode.wolf2w.listener;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.test.RedisByKey;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.StrategyStatisVO;
import com.sun.javafx.scene.control.skin.PaginationSkin;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

//@Component
public class RedisDateInitListenter implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private StringRedisTemplate template;
    //当spring容器启动之后马上执行该方法，在该方法中对攻略vo对象的初始化
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        ////从mongodb中查询出所有的攻略
        //List<Strategy> strategies = strategyService.selectAll();
        ////便利所有的攻略，封装成vo对象
        //for(Strategy strategy:strategies){
        //    //获取vo对象的key
        //    String key = RedisByKey.STRATEGY_STATIC_VO.join(strategy.getId());
        //    //判断vo已经初始化过了
        //    if(template.hasKey(key)){
        //        continue;
        //    }
        //    //获取vo对象
        //    StrategyStatisVO vo = strategyService.getStrategyStatisVO(strategy);
        //    //初始化vo中的字段值
        //    BeanUtils.copyProperties(strategy, vo);
        //    vo.setStrategyId(strategy.getId());
        //    //将vo缓存到redis对象中去
        //    strategyService.setStrategyStatisVO(strategy, vo);
        //}


        //将所有用户各自对应的全部攻略分别初始化到redis中去
        ////1、查出所有用户的id
        //List<UserInfo> userInfos = userInfoService.selectAll();
        ////创建一个封装所有用户的id的集合
        //List<String> ids=new ArrayList<>();
        //for(UserInfo userInfo:userInfos){
        //    String id = userInfo.getId();
        //    ids.add(id);
        //}
        ////2、便利再查出所有用户所对应的攻略id
        //for(String id:ids){
        //
        //}
        ////3、再拼接成所对应的key，判断vo是否已经被初始化了，然后获取vo对象，然后获取vo对象，初始化vo中的字段值，封装到redis对象中去
    }
}
